﻿require([
"jquery",
"ko",
"bootstrap",
"bootstrap-datetimepickerlocal",
"akcy"], function ($, ko) {
    var akcy = arguments[arguments.length - 1];
    akcy.ui.init();

    var sessionid = akcy.session.get();
    var username = window.parent.currentuser().username;

    $(".left-btn").click(function () {
        $(".inbox-content-child").hide();
        var id = $(this).attr("data-target");
        $("#" + id).show();
        $(".inbox-nav>li").removeClass("active");
        $(this).parent().addClass("active");

        var title = $(this).attr("title");
        $(".pull-left").html(title);
        
        if (title == "收件箱")
        {
            table.getInstance().Msg_IsUnread(false);
            table.load();
        }
        if (title == "未读的")
        {
            table.getInstance().Msg_IsUnread(true);
            table.load();
        }
    });

    var form = akcy.form().options({
        fields: [
            { field: 'MSG_ID', value: ko.observable(0) },
            { field: 'MSG_SenderName', value: ko.observable(username) },
            { field: 'MSG_Type', value: ko.observable() },
            { field: 'MSG_Title', value: ko.observable() },
            { field: 'MSG_SendDate', value: ko.observable() },
            { field: 'MSG_Url', value: ko.observable() },
            { field: 'MSG_Content', value: ko.observable() }
        ],
        dataAddUrl: '/ak56api/Hubs/PushSystemMsg'
    }).optionsExtend({
        added: function () {
            table.load();
            akcy.alert.success("发布公告成功！");
        }
    }).bind("msgform");


    var table = akcy.table().options({
        fields: [
            { field: 'Msg_Receiver', value: ko.observable(sessionid), method: akcy.querymethod.equal },
            { field: 'Msg_IsUnread', value: ko.observable(false), method: akcy.querymethod.equal }
        ],
        defaultOrderBy: "MSG_SendDate",
        dataQueryUrl: '/ak56api/Hubs/GetMsgsForPaging'
    }).optionsExtend({
        isCheckedAll: ko.observable(false),
        checkedAll: function () {
            var array = [];
            if (table.getInstance().isCheckedAll() == false) {
                table.getInstance().isCheckedAll(true);
                $(table.getInstance().recordSet()).each(function () {
                    array.push($(this)[0].MSG_ID);
                });
            } else {
                table.getInstance().isCheckedAll(false);
            }
            table.getInstance().checkedData(array);
        },
        checkedData: ko.observableArray(),
        batchRead: function () {
            var msgids = table.getInstance().checkedData();
            if (msgids.length == 0) {
                akcy.alert.warning("未选择项！");
                return;
            }
            msgids = msgids.join(",");
            akcy.ajaxpost("/ak56api/Hubs/ReadSystemMsg?msgs=" + msgids + "&sessionid=" + sessionid).done(function () {
                table.load();
                table.getInstance().checkedData([]);
            });
        },
        searched: function () {
            $(".inbox-tr").bind('click', function () {
                $(this).next().fadeToggle();
                $(this).closest("tr").removeClass("unread");
                var msgid = $(this).closest("tr").attr("msgid");
                akcy.ajaxpost("/ak56api/Hubs/ReadSystemMsg?msgs=" + msgid + "&sessionid=" + sessionid);
            })

            $(".inbox-tr input:checkbox").bind('click', function (event) {
                event.stopPropagation();
            })
        }
    }).bind("msglist").load();

});